package com.widget.wallpaper;

import java.io.IOException;
import java.util.Random;


import android.app.Service;
import android.app.WallpaperManager;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;

public class WallpaperService extends Service {
    private Integer [] mImageIds = { 
            R.drawable.wallpaper1,
            R.drawable.wallpaper2,
            R.drawable.wallpaper3,
            R.drawable.wallpaper4           };
    private static final Random rgenerator = new Random();
    /* try to get the clear current wallpaper; currently wallpaper is randomized; try to fix by using current wallpaper  */
//    private Drawable currentWallpaper = new Drawable();
	@Override
	public void onStart(Intent intent, int startId) {

		RemoteViews views = new RemoteViews(this.getPackageName(),R.layout.main);
		// Build the widget update.
		WallpaperManager myWallpaperManager
	     = WallpaperManager.getInstance(getApplicationContext());
		
		
		try {
			myWallpaperManager.clear();
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//			currentWallpaper = myWallpaperManager.getDrawable();
	    //get string of preference from input box and convert to resource id	
		
		
		
		Drawable image = getResources().getDrawable(mImageIds[rgenerator.nextInt(mImageIds.length)]);
		//Drawable image =myWallpaperManager.getDrawable();

		Bitmap bitmapImg = ((BitmapDrawable)image).getBitmap();
		
		// Store our image size as a constant
		final int IMAGE_WIDTH = image.getIntrinsicWidth();
		final int IMAGE_HEIGHT = image.getIntrinsicHeight();
		
		
		//get character from widget to draw on wallpaper
		Resources res = getResources();
		String mDrawableName = "@drawable/"+WNWService.getCharacterWallpaper(this)+"1";
		int resID = res.getIdentifier(mDrawableName, "drawable", this.getPackageName());
		
		Drawable chacterImage = getResources().getDrawable(resID);
		Bitmap bitmapCharImg = ((BitmapDrawable)chacterImage).getBitmap(); //convert to bitmap
		// You can also use Config.ARGB_4444 to conserve memory or ARGB_565 if 
		// you don't have any transparency.
		Bitmap canvasBitmap = Bitmap.createBitmap(IMAGE_WIDTH, 
		                                          IMAGE_HEIGHT, 
		                                          Bitmap.Config.ARGB_8888);
		// Create a canvas, that will draw on to canvasBitmap. canvasBitmap is
		// currently blank.
		Canvas imageCanvas = new Canvas(canvasBitmap);
		
		// Set up the paint for use with our Canvas
		Paint imagePaint = new Paint();
		imagePaint.setTextAlign(Align.CENTER);
		imagePaint.setTextSize(14f);
		imagePaint.setColor(Color.GRAY);
		imagePaint.setFakeBoldText(true);
		// Draw the image to our canvas
		imageCanvas.drawBitmap(bitmapImg, 0, 0, null);
		//image.draw(imageCanvas);
		Rect recImg = new Rect();
		recImg.set(IMAGE_WIDTH /2, IMAGE_HEIGHT /2, (IMAGE_WIDTH /2)+66, (IMAGE_HEIGHT /2)+66);
		imageCanvas.drawBitmap(bitmapCharImg, null, recImg, null);
		//imageCanvas.drawColor(Color.YELLOW);
		// Draw the text on top of our image
		String backgroundStr = inputActivity.getBackgroundPref(this.getBaseContext());
		
		
		
		imageCanvas.drawText(backgroundStr, 
				IMAGE_WIDTH /2, 
                IMAGE_HEIGHT / 2, 
                imagePaint);
		// This is the final image that you can use 
		
		//BitmapDrawable finalImage = new BitmapDrawable(canvasBitmap);


		
		
		//create ImageView
		
		//views.setImageViewBitmap(R.id.imageView1, bitmap);
		
		try {
			myWallpaperManager.setBitmap(canvasBitmap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
	     //myWallpaperManager.setResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);
	     Log.d("check_point",rgenerator.nextInt(mImageIds.length)+"");
	    
	   
		// Push update for this widget to the home screen
		ComponentName WNWidget = new ComponentName(this,
				WNWProvider.class);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
		appWidgetManager.updateAppWidget(WNWidget, views);
		//Log.d("check_point","wallpaper");
	}
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
